%make background  database
YearMonthDay=20190327;
photoStart=1;
photoEnd=250;
mode=0;
photoBEC='\\RB87BEC2\';
if strcmp(photoBEC,'\\Camera-PC\')
    photoNamePre='4552test_';
    filename_pre='ascOD4552test_';
    resolution=512;
    folderName=[photoBEC 'ShareFolder\' num2str(YearMonthDay) 'morning\4552\'];
elseif strcmp(photoBEC,'\\RB87BEC2\')
    photoNamePre='22834test_';
    filename_pre='ascOD22834test_';
    resolution=1024;
    folderName=[photoBEC 'ShareFolder\' num2str(YearMonthDay) 'morning\ikon-22834\'];
end
refimagesIniDirectory='D:\Rb87ProgramBEC3\removeBackground\backgrounddata\refimagesIni_22834new.mat';
refNum=250;
refInd=photoStart:(photoStart+refNum-1);
if mode==0
    if exist(refimagesIniDirectory,'file')==2
        refimagesIniStruct=load(refimagesIniDirectory);
        refimagesIni=refimagesIniStruct.refimagesIni;
        
        if size(refimagesIni,3)==refNum
            refimages=refimagesIni;
        else
            error('Error: refNum ~= loaded reference images');
        end
    end
    
    
    refInd=photoStart:min((photoStart+refNum-1),photoEnd);
    refimagesIni(:,:,mod(refInd,refNum)+1)=refCreate(refInd,'bg',folderName,photoNamePre,resolution);
    save(refimagesIniDirectory','refimagesIni');
end